# -*- coding: utf-8 -*-
'''
@文件    :myconf.py
@说明    :用户相关操作
@时间    :2023/12/22 00:49:45
@作者    :浪晋
@版本    :1.0
'''

from app.admin import auth
from flask import request,g
from utils.requetFunc import RequestParser,pageConvert
from utils.responseFunc import resultMsg
from utils.commomFunc import build_tree
from json import loads


@auth.route("/myself/menu",methods=["get"])
def menu_myself():
    """
    @功能    :自己的菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    roleId = g.userInfo.get("roleId")
    sql = "select a.* from tb_system_menu a join tb_system_permission b on a.id = b.menuId where b.roleId = %s  and a.status = 1;"
    result = []
    permissions = []
    for menu in db.fetchall(sql,(roleId)):
        menu.update(meta=loads(menu["meta"]) )
        result.append(menu)
    permissions = [i["name"] for i in result if i["meta"]["type"] == "button"]
    menu = build_tree(result)
    sql = "select a.* from tb_system_menu a join tb_system_role b on a.roleId = b.id where c.roleId = %s;"
    result = {
        "menu":menu,
        "permissions":permissions,
        "dashboardGrid":['welcome','echarts','about', 'ver','time','progress']
    }
    return resultMsg(data=result)


@auth.route("/myself/detail",methods=["get"])
def myself_detail():
    """
    @功能    :自己的资料
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    userId = g.userInfo.get("id")
    sql = "select id,account,avatar,nickName,gender,phone,about from tb_system_user where id = %s;"
    result = db.fetchone(sql,(userId))
    return resultMsg(data=result)


@auth.route("/myself/edit",methods=["post"])
def myself_edit():
    """
    @功能    :修改自己的资料
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    userId = g.userInfo.get("id")
    body = request.get_json()
    avatar = body.get("avatar")
    nickName = body.get("nickName")
    gender = body.get("gender")
    phone = body.get("phone")
    about = body.get("about")
    parse = RequestParser()
    parse.add_argument(value=avatar,name="头像",required=True,remark="必填")
    parse.add_argument(value=nickName,name="昵称",required=True,remark="必填")
    parse.add_argument(value=gender,name="性别",required=True,remark="必填")
    parse.add_argument(value=phone,name="电话",required=True,remark="必填")
    parse.add_argument(value=about,name="签名",required=True,remark="必填")
    parseMsg = parse.parse_args()
    if parseMsg :
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_user set avatar=%s,nickName=%s,gender=%s,phone=%s,about=%s where id = %s;"
    db.execute(sql,(avatar,nickName,gender,phone,about,userId))
    db.commit()
    return resultMsg()


@auth.route("/myself/repw",methods=["post"])
def myself_repw():
    """
    修改密码
    """
    db = g.db
    userId = g.userInfo.get("id")
    body = request.get_json()
    oldPassword = body.get("oldPassword")
    newPassword = body.get("newPassword")
    parse = RequestParser()
    parse.add_argument(value=oldPassword,name="旧密码",required=True,remark="必填")
    parse.add_argument(value=newPassword,name="新密码",required=True,remark="必填")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "select password from tb_system_user where id = %s;"
    result = db.fetchone(sql,(userId,))
    if oldPassword == result["password"]:
        sql = "update tb_system_user set password = %s where id = %s;"
        db.execute(sql,(newPassword,userId))
        db.commit()
        return resultMsg(msg="操作成功")
    else:
        return resultMsg(code=0,msg="密码错误")

    